package com.psct.social;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.psct.R;
import com.tencent.weibo.oauthv2.OAuthV2;
import com.weibo.sdk.android.Oauth2AccessToken;



/**
 * �����¼���ϴ����࣬�ṩ���汾�غ�ȡ������
 * 
 * @author zjingchuan
 * 
 */
public class SocialAccountKeeper {
	private static final String PREFERENCES_NAME = "accesstoken";

	/**
	 * ���������ʺŵ�AccessToken
	 * @param context
	 * @param token
	 */
	public static void keepAccessToken(Context context, Oauth2AccessToken token) {
		SharedPreferences pref = context
				.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor editor = pref.edit();
		editor.putString("sina_token", token.getToken());
		editor.putLong("sina_expiresTime", token.getExpiresTime());
		editor.commit();
	}

	/**
	 * ȡ�������ʺŵ�AccessToken
	 * @param context
	 * @return ���û�б�����򷵻�null
	 */
	public static Oauth2AccessToken getSinaAccessToken(Context context) {
		
		SharedPreferences pref = context
				.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		String tokenStr = pref.getString("sina_token", "");
		long expiresTimeStr = pref.getLong("sina_expiresTime", 0);
		if(!TextUtils.isEmpty(tokenStr)&&expiresTimeStr!=0) {
			Oauth2AccessToken token = new Oauth2AccessToken();
			token.setToken(tokenStr);
			token.setExpiresTime(expiresTimeStr);
			return token;
		}
		return null;
	}
	/**
	 * ɾ�������ʺŵ���Ϣ
	 */
	public static void removeSinaInfo(Context context) {
		SharedPreferences pref = context
				.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor editor = pref.edit();
		editor.putString("sina_token","");
		editor.putLong("sina_expiresTime",0);
		editor.putString("sina_user_name","");
		editor.putString("sina_image_file","");
		editor.commit();
	}
	/**
	 * ɾ����Ѷ�ʺŵ���Ϣ
	 */
	public static void removeTenceInfo(Context context) {
		SharedPreferences pref = context
				.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor editor = pref.edit();
		editor.remove("tence_token");
		editor.remove("tence_expiresTime");
		editor.remove("tence_openkey");
		editor.remove("tence_image_file");
		editor.remove("tence_user_name");
		editor.remove("tence_openid");
		editor.commit();
	}
	/**
	 * ��������ʺŵ��û���Ϣ����ʾ�ã�
	 * @param context
	 * @param user
	 */
	public static void keepUserInfo(Context context,String type,String name,String imagePath) {
		SharedPreferences pref = context
				.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor editor = pref.edit();
		editor.putString(type+"_user_name", name);
		editor.putString(type+"_image_file", imagePath);
		editor.commit();
	}

	/**
	 * ȡ�ñ���İ󶨵ĵ����ʺŵ���ʾ��Ϣ
	 * @param context
	 * @param type
	 * @return
	 */
	public static SocialAccount getUserInfo(Context context,String type) {
		SocialAccount info = null ;
		if(type.equals("sina")) {
			info = new SinaAccount();
			info.typeName="新浪微博";
			info.ico=R.drawable.sina_ico;
		}else if(type.equals("tence")) {
			info = new TenceAccount();
			info.typeName="腾讯微博";
			info.ico=R.drawable.tence_ico;
		}
		SharedPreferences pref = context
				.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		String name = pref.getString(type+"_user_name", "");
		String image=pref.getString(type +"_image_file","");
		if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(image)){
			info.name=name;
			info.iamge_file=image;
			info.isBind=true;
		}
		return info;
	}
	/**
	 * ����������
	 * @param context
	 */
	public static void clear(Context context) {
		SharedPreferences pref = context
				.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor editor = pref.edit();
		editor.clear();
		editor.commit();

	}

	/**
	 * ������Ѷ΢���ʺŵ�oAuthtoken
	 * @param context
	 * @param oAuth
	 */
	public static void keepAccessToken(Context context, OAuthV2 oAuth) {
		SharedPreferences pref = context
				.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor editor = pref.edit();
		editor.putString("tence_token", oAuth.getAccessToken());
		editor.putString("tence_expiresTime", oAuth.getExpiresIn());
		editor.putString("tence_openkey", oAuth.getOpenkey());
		editor.putString("tence_openid", oAuth.getOpenid());
		editor.commit();
	}
	
	/**
	 * ��ȡ��Ѷ΢���ʺŵ�oAuthtoken
	 * @param context
	 * @return
	 */
	public static OAuthV2 getTenceOAuth(Context context) {
		SharedPreferences pref = context
				.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		String token = pref.getString("tence_token", "");
		String expiresTime = pref.getString("tence_expiresTime", "");
		String openkey = pref.getString("tence_openkey", "");
		String openid = pref.getString("tence_openid", "");
		if(!TextUtils.isEmpty(token)) {
			OAuthV2 oAuth = new OAuthV2();
			oAuth.setAccessToken(token);
			oAuth.setExpiresIn(expiresTime);
			oAuth.setOpenid(openid);
			oAuth.setOpenkey(openkey);
			oAuth.setClientId(TenceAccount.TENCE_CONSUMER_KEY);
			return oAuth;
		}
		return null;
	}
	
    
    /** 
     * ��ȡ�û�ip 
     * @return 
     */  
    public static String getHostIp() {    
        try {  
            for (Enumeration<NetworkInterface> en = NetworkInterface  
                    .getNetworkInterfaces(); en.hasMoreElements();) {  
                NetworkInterface intf = en.nextElement();  
                for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr  
                        .hasMoreElements();) {  
                    InetAddress inetAddress = ipAddr.nextElement();  
                    if (!inetAddress.isLoopbackAddress()) {                       
                        return inetAddress.getHostAddress();  
                    }  
                }  
            }  
        } catch (SocketException ex) {  
        } catch (Exception e) {  
        }  
        return null;  
    }  
}
